<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>

        window.onload = function(){
            function foo(){
                alert('foo');
            }
            var b = document.getElementById('box');
            alert(b);
            foo();
            //            window.foo = foo;//将foo提示到全局下
            var btn = document.getElementById('btn');
            btn.onclick = foo;
        };


        document.onready = function(){
            function foo(){
                alert('foo');
            }
            var b = document.getElementById('box');
            alert(b);
            foo();
            //            window.foo = foo;//将foo提示到全局下
            var btn = document.getElementById('btn');
            btn.onclick = foo;
        }
    </script>
</head>
<body>
<script>

</script>
<div id="box" style="background-color:red;width: 100px;height: 100px"></div>
<input type="button" id="btn" value="点我">

</body>
</html>